import {
    Data
} from "../../modeal/data.js";

import {
    Shop
} from "../../modeal/shop.js";
import {
    showToast
} from "../../utils/ui.js"
const data = new Data();

Page({
    data: {
        shops: [],
        locationText: "正在获取您的位置..."
    },

    onLoad() {
        const loacation = wx.getStorageSync('location')
        this._getShops(loacation.latitude, loacation.longitude)
    },



    // 获取附近门店
    async _getShops(latitude, longitude) {
        const res = await Shop.getShops(latitude, longitude)
        let shops = []
        res.data.list.forEach(item => {
            item.types.indexOf(1) != -1 ? item.eatIn = true : ""
            item.types.indexOf(2) != -1 ? item.ziti = true : ""
            item.types.indexOf(3) != -1 ? item.takeOut = true : ""
            shops.push(item)
        });
        this.setData({
            shops: shops,
            locationText: res.data.location_address
        })
    },

    // 联系商家
    call(e) {
        let phoneNumber = e.currentTarget.dataset.telphone;
        wx.makePhoneCall({
            phoneNumber
        });
    },

    goShoping(e) {
        const shop = e.currentTarget.dataset.shop;
        wx.setStorageSync('shopInfo', shop)
        if (shop.IS_Close) {
            showToast(shop.shopname + '已打烊')
            return
        }
        wx.navigateTo({
            url: "/pages/shopping/shoping"
        });
    }

})